<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格    -->
            <el-table ref="refrole" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="dofile(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hedit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hdel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :visible.sync="showdialog " title="弹窗" @close="resetform">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog :visible.sync="showsetrole" title="分配权限">
        <assignpermiss v-if="showsetrole" :id="curid" @success="hsuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getrolelist, addrolelist, editrolelist, delrolelist } from '@/api/settings'
import assignpermiss from '@/views/settings/assignPermission.vue'
export default {
  name: 'VueHrSettings',
  components: {
    assignpermiss
  },
  data() {
    return {
      curid: '',
      isEdit: false,
      showdialog: false,
      showsetrole: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      },
      list: [],
      total: 0,
      query: {
        page: 1,
        pagesize: 2
      }
    }
  },

  mounted() {

  },
  created() {
    this.loadrolelist()
  },

  methods: {
    // 获取全部角色信息 初始化页面
    async loadrolelist() {
      try {
        const res = await getrolelist(this.query)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      this.query.page = 1
      console.log(`每页 ${val} 条`)
      this.query.pagesize = val
      this.loadrolelist()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.query.page = val
      this.loadrolelist()
    },
    // 新增角色
    async addlist() {
      try {
        this.total++
        const maxpage = Math.ceil(this.total / this.query.pagesize)
        this.query.page = maxpage
        this.showdialog = true
        const res = await addrolelist(this.roleForm)
        this.loadrolelist()
        this.showdialog = false
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.isEdit ? this.editlist() : this.addlist()
    },
    hadd() {
      this.isEdit = false
      this.showdialog = true
    },
    hedit(row) {
      this.isEdit = true
      this.showdialog = true
      this.roleForm = { ...row }
    },
    async editlist(id) {
      try {
        const res = await editrolelist(this.roleForm)
        console.log(res)
        this.loadrolelist()
        this.showdialog = false
      } catch (error) {
        console.log(error)
      }
    },
    hdel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delrolelist(id)
          console.log(res)
          if (this.list.length) {
            this.query.page--
            if (this.query.page <= 0) {
              this.query.page = 1
            }
          }
          this.loadrolelist()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    resetform() {
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    dofile(id) {
      this.showsetrole = true
      this.curid = id
    },
    hsuccess() {
      this.showsetrole = false
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
